<?php

/**
 * @category   MM
 * @package    MM_Service
 */
class MM_Service_Admin_EventApplause extends Mediad2_Service_Abstract
{
	/**
	 * 初始化
	 */
	public function init()
	{
		$this->setMapper('MM_Mapper_EventApplause');
		$this->setModel('MM_Model_EventApplause');
	}
	
	/**
	 * 增加Applause
	 * @param int $eid
	 * @param string $objecttype
	 * @param int $objectid
	 * @param string $applausetype
	 * @param int $number
	 * @return Mediad2_Model_Result
	 */
	public function add($eid, $objecttype, $objectid, $applausetype="hand", $number=1)
	{
		//事务处理
		$db1 = Zend_Registry::get('db1');
		$db1->beginTransaction();
		
		try {
			
			//封装where
			$where = "eid = $eid AND objecttype = '$objecttype' AND objectid = $objectid";
			
			//查找是否存在指定条件的记录
			$rows = $this->getMapper()->getCount($where);
			if ($rows==0) {	//如果不存在指定记录，则新增一条记录
				$this->create($eid, $objecttype, $objectid);
			}
			//更新+1
			switch ($applausetype) {
				case "hiss":
					$strExpr = "hiss + $number";
					$row = array(
							'hiss' => new Zend_Db_Expr($strExpr),
					);
					break;
				case "flower":
					$strExpr = "flower + $number";
					$row = array(
							'flower' => new Zend_Db_Expr($strExpr),
					);
					break;
				case "hand":
				default:
					$strExpr = "hand + $number";
					$row = array(
							'hand' => new Zend_Db_Expr($strExpr),
					);
					break;
			}
			$this->getMapper()->__updateSQL($row, $where);
		
			//提交事务
			$db1->commit();
		
			//返回结果--成功
			return $this->returnResult(Mediad2_Model_Result::CODE_SUCCESS);
		
		} catch (Zend_Exception $e) {
		
			//回滚事务
			$db1->rollback();
		
			//出错处理
			$this->handleException($e);
		
			//返回结果--失败
			return $this->returnResult(Mediad2_Model_Result::CODE_FAILURE);
		}
	}
	
	/**
	 * create 
	 * @param int $eid
	 * @param string $objecttype
	 * @param int $objectid
	 * @return Mediad2_Model_Result
	 */
	public function create($eid, $objecttype, $objectid)
	{
		$model = new MM_Model_EventApplause();
		$model->setEid($eid);
		$model->setObjecttype($objecttype);
		$model->setObjectid($objectid);
		$model->setHand(0);
		$model->setHiss(0);
		$model->setFlower(0);
		$model->setLocalhand(0);
		$model->setLocalhiss(0);
		$model->setLocalflower(0);
		return $this->insert($model);
	}
}